var express = require("express");
var router = express.Router();
const fs = require("fs");
const { uploadFiles, moveFiles, removeFiles } = require("../utils/uploadFiles");

const {
  deletOne2,
  addOne2,
  getAll2,
  getOne2,
  updateOne2,
  getTypes2,
  updateMany2
} = require("../service/goodsService");
//渲染数据
router.get("/getAll", async (req, res) => {
  const page = req.query;

  const data = await getAll2(page);
  console.log(data);
  res.send(await getAll2(page));
});
// 删除数据
router.post("/del", async (req, res) => {
  const goodsId = req.body;
  const data = await deletOne2(goodsId);
  res.send(data);
});
//新增数据
router.post("/add", async (req, res) => {
  const goods = req.body;
  const data = await addOne2(goods);
  const { listImage, detailImage } = req.body;
  console.log(listImage, detailImage);

  // if(imgs){
  if (listImage.length > 0) {
    for (let i in listImage) {
      moveFiles({
        fromPath: "./public/temp",
        toPath: "./public/files",
        filename: listImage[i],
      });
    }
  }
  if (detailImage.length > 0) {
    for (let j in detailImage) {
      moveFiles({
        fromPath: "./public/temp",
        toPath: "./public/files",
        filename: detailImage[j],
      });
    }
  }

  if (data) {
    // if(imgs){

    removeFiles("./public/temp");
    // }
  }
  res.send(data);
});
//修改  1.获取
router.get("/getOne", async (req, res) => {
  const _Id = req.query;
  res.send(await getOne2(_Id));
});
//修改 2.确认修改
router.post("/updateOne", async (req, res) => {
  const update = req.body;
  // console.log('123123123', update);

  const { listImage, detailImage, delDetailImg, delListImg } = req.body;

  // 删除原先图片

  if (delDetailImg.length > 0) {
    for(let i in delDetailImg){

      fs.stat("./public/files/" + delDetailImg[i], function (err, stat) {
        if (stat) {
          fs.unlinkSync("public/files/" + delDetailImg[i], (err) => {
            if (err) {
              console.log(err);
              res.send(err);
            } else {
              res.send("删除成功");
            }
          });
        } else {
          console.log("文件不存在");
        }
      });
    }
  }
  if (delListImg.length > 0) {
    for(let i in delListImg){

      fs.stat("./public/files/" + delListImg[i], function (err, stat) {
        if (stat) {
          fs.unlinkSync("public/files/" + delListImg[i], (err) => {
            if (err) {
              console.log(err);
              res.send(err);
            } else {
              res.send("删除成功");
            }
          });
        } else {
          console.log("文件不存在");
        }
      });
    }
  }
  

  
  // 传进去的图片后判断是否在temp里面，可以不判断，直接删
  // if (listImage.length > 0) {
  for (let i in listImage) {
    fs.stat("./public/temp/" + listImage[i], function (err, stat) {
      // console.log(123123123123, listImage[i], data);
      if (!err) {
        moveFiles({
          fromPath: "./public/temp",
          toPath: "./public/files",
          filename: listImage[i],
        });
      }
    });
  }
  // }

  for (let j in detailImage) {
    fs.stat("./public/temp/" + detailImage[j], function (err, stat) {
      if (!err) {
        moveFiles({
          fromPath: "./public/temp",
          toPath: "./public/files",
          filename: detailImage[j],
        });
      }
    });
  }

  const data = await updateOne2(update);
  if (data) {
    fs.access("./public/temp", function (err) {
      console.log(!err ? "存在" : "没有创建");
      if (!err) {
        removeFiles("./public/temp");
        console.log("temp 删除成功");
      } else {
      }
    });
  }
  // if(imgs){

  res.send(data);
});
// 改多条 用于上架
router.post("/updatemany", async (req, res) => {
  const update = req.body;
  console.log(update);
  
  res.send(await updateMany2(update));
});



router.get("/getTypes", async (req, res) => {
  const _Id = req.query;
  res.send(await getTypes2(_Id));
});

// 上传
const images = uploadFiles();
// path: "/public/temp",
// key: "file",
router.post("/uploadImages", async function (req, res, next) {
  // res.send(req.files[0].filename);
  images(req, res, function (err) {
    if (err) {
      if (err.code === "LIMIT_FILE_SIZE") {
        res.send({ success: false, msg: "文件过大" });
      }
    } else {
      res.send({ success: true, filename: req.files[0].filename });
    }
  });
});
router.post("/cacheImages", images, async function (req, res, next) {
  res.send(req.files[0].filename);
});

//删除 图片 暂时没用！
router.post("/deleteImages", async function (req, res, next) {
  // res.send(req.files[0].filename);
  // console.log("imgs",req.imgs);
  console.log(12321, req.body);

  
});

module.exports = router;
